export default {
    data() {
        return {
            // 添加弹框
            dialogAdd: false,
            // 修改弹框
            dialogUpd: false,
            // table-tree组件配置项
            columns: [{
                label: '分类名称',
                prop: 'cat_name'
            }, {
                label: '是否有效',
                prop: 'cat_deleted',
                type: 'template',
                template: 'isEffect'
            }, {
                label: '排序',
                prop: 'cat_level',
                type: 'template',
                template: 'sort'
            }, {
                label: '操作',
                type: 'template',
                template: 'operate'
            }],
            // 分类数据
            categoriesList: [],
            // 查询参数
            queryInfo: {
                type: 3,
                pagenum: 0,
                pagesize: 5
            },
            // 总数据
            total: 0,
            // 添加form表单
            addCategoriesForm: {
                cat_name: ''
            },
            // 添加分类时的分类数据
            searchCatLsit: [],
            // 下拉选中后的数据
            cascaderArr: [],
            cascaderProps: {
                label: 'cat_name',
                value: 'cat_id',
                children: 'children',
                expandTrigger: 'hover'
            },
            // 校验规则
            rules: {
                cat_name: [{
                    required: true,
                    message: '请输入分类名称',
                    trigger: 'blur'
                }]
            },
            // 更新分类当前id
            catId: ''
        }
    },
    created() {
        this.getCategoriesList();
    },
    methods: {
        // 查询所有分类数据
        async getCategoriesList() {
            let {
                data: res
            } = await this.$http.get('categories', {
                params: this.queryInfo
            });
            if (res.meta.status != 200) return this.$message.error('获取分类数据失败');
            this.categoriesList = res.data.result;
            this.total = res.data.total;
        },
        // 分页方法
        handleSizeChange(val) {
            // console.log(`每页 ${val} 条`);
            this.queryInfo.pagesize = val;
            this.getCategoriesList();
        },
        handleCurrentChange(val) {
            // console.log(`当前页: ${val}`);
            this.queryInfo.pagenum = val;
            this.getCategoriesList();
        },
        // 查询分类
        async searchCat() {
            this.dialogAdd = true;
            let {
                data: res
            } = await this.$http.get('categories', {
                params: {
                    type: 2
                }
            });
            this.searchCatLsit = res.data;
        },
        // 添加分类
        addCat() {
            this.$refs.addCategoriesForm.validate(async valid => {
                if (!valid) return;
                if (this.cascaderArr.length == 0) {
                    let {
                        data: res
                    } = await this.$http.post('categories', {
                        cat_pid: this.cascaderArr.length,
                        cat_name: this.addCategoriesForm.cat_name,
                        cat_level: this.cascaderArr.length
                    });
                    if (res.meta.status != 201) return this.$message.error('添加失败');
                } else {
                    let {
                        data: res
                    } = await this.$http.post('categories', {
                        cat_pid: this.cascaderArr[this.cascaderArr.length - 1],
                        cat_name: this.addCategoriesForm.cat_name,
                        cat_level: this.cascaderArr.length
                    });
                    if (res.meta.status != 201) return this.$message.error('添加失败');
                }
                this.$message.success('添加商品分类成功');
                this.dialogAdd = false;
                this.getCategoriesList();
                this.$refs.addCategoriesForm.resetFields();
            })
        },
        // 根据 id 查询分类
        async searchIdCat(id) {
            this.catId = id;
            this.dialogUpd = true;
            let {
                data: res
            } = await this.$http.get('categories/' + id);
            // console.log(res)
            if (res.meta.status != 200) return this.$message.error('查询分类失败');
            this.addCategoriesForm = res.data;
        },
        // 编辑提交分类
        submitCat() {
            this.$refs.addCategoriesForm.validate(async valid => {
                if (!valid) return;
                let {
                    data: res
                } = await this.$http.put('categories/' + this.catId, this.addCategoriesForm);
                // console.log(res);
                if (res.meta.status != 200) return this.$message.error('修改分类失败');
                this.getCategoriesList();
                this.dialogUpd = false;
                this.$message.success('修改成功');
            })
        },
        // 删除分类
        async deleteCat(id) {
            let confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(error => error);
            // console.log(confirm)
            if (confirm == 'cancel') return this.$message('取消成功');
            let {
                data: res
            } = await this.$http.delete('categories/' + id);
            // console.log(res)
            if (res.meta.status != 200) return this.$message.error('删除失败');
            this.getCategoriesList();
            this.$message.success('删除成功');
        }
    },
}